﻿using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    public class DefaultController : AbpController
    {
        /// <summary>
        /// 获取调用返回
        /// </summary>
        protected async Task<OkObjectResult> GetInvokeAsync<TResult>(Func<Task<TResult>> func)
        => Ok(await func());

        /// <summary>
        /// 插入调用返回
        /// </summary>
        protected async Task<CreatedAtRouteResult> InsertInvokeAsync<TResult>(
            Func<Task<TResult>> func,
            string routeName,
            Func<TResult, object> getRouteValues)
        {
            var result = await func();
            var routeValues = getRouteValues(result);
            return CreatedAtRoute(routeName, routeValues, result);
        }

        /// <summary>
        /// 全量更新返回
        /// </summary>
        protected async Task<OkObjectResult> PutInvokeAsync<TResult>(Func<Task<TResult>> func)
             => Ok(await func());

        /// <summary>
        /// 全量更新返回
        /// </summary>
        protected async Task<NoContentResult> PutInvokeAsync<TResult>(Func<Task> func)
        {
            await func();
            return NoContent();
        }

        /// <summary>
        /// 执行业务逻辑操作返回
        /// </summary>
        protected async Task<OkResult> ActionInvokeAsync(Func<Task> action)
        {
            await action();
            return Ok();
        }

        /// <summary>
        /// 删除操作返回
        /// </summary>
        protected async Task<NoContentResult> DeleteInvokeAsync(Func<Task> action)
        {
            await action();
            return NoContent();
        }

        /// <summary>
        /// 不定操作返回
        /// </summary>
        protected async Task<OkObjectResult> PatchInvokeAsync<TResult>(Func<Task<TResult>> func) => Ok(await func());
    }
}
